//
//  Tool.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/11/15.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "Tool.h"
#import "YYReachability.h"


@implementation Tool
//view 生成清晰的图片
// 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
+ (UIImage*) imageWithUIView:(UIView*) view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale);
//    UIGraphicsBeginImageContext(view.bounds.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tImage;
}

+ (NSString *)getHeadNameWithArr:(NSArray *)arr withString:(NSString *)str{
    
    NSString *nameStr = @"";
    for (int i = 0; i<arr.count; i++) {
        
        NSDictionary *dic = arr[i];
        
        if ([dic[@"loginName"] isEqualToString:str]) {
            nameStr = [dic valueForKey:@"name"];
            
            break;
        }
    }
    if ([nameStr isEqualToString:@""]|| !nameStr) {
        nameStr = str;
    }
    return nameStr;
}

+ (NSString *)unChiveWithString:(NSString *)str{
    NSData *data = [[NSUserDefaults standardUserDefaults]valueForKey:@"numberArr"];
    NSArray *numberSArr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    NSString *nameStr = @"";
    for (int i = 0; i<numberSArr.count; i++) {
        
        NSDictionary *dic = numberSArr[i];
        
        if ([dic[@"loginName"] isEqualToString:str]) {
            nameStr = [dic valueForKey:@"name"];
          
            break;
        }
    }
    if ([nameStr isEqualToString:@""]|| !nameStr) {
        nameStr = str;
    }
    return nameStr;
    
}

+ (NSString *)numberToStrWithNum:(NSNumber *)value{
    NSInteger i = [value integerValue];
    NSString *str = [NSString stringWithFormat:@"%ld",i];
    return str;
}

//iOS 时间戳转换为字符串标准时间
+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd  HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    //截取字符串，变成MM-dd- HH:mm
   dateString = [dateString substringFromIndex:5];
    return dateString;
}

+ (CGSize) boundingRectWithSize:(NSString*) txt Font:(UIFont*) font Size:(CGSize) size
{
    
    CGSize _size;
    
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    
    NSDictionary *attribute = @{NSFontAttributeName: font};
    
    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
    
    NSStringDrawingUsesLineFragmentOrigin |
    
    NSStringDrawingUsesFontLeading;
    
    _size = [txt boundingRectWithSize:size options: options attributes:attribute context:nil].size;
    
#else
    
    _size = [txt sizeWithFont:font constrainedToSize:size];
    
#endif
    
    return _size;
    
}

#pragma mark - 检测网络状态
+ (BOOL)showNoNetwork:(UIViewController *)vc {
    // 检测网络状态
    if ([[YYReachability reachability] isReachable] == NO) {
        
        
        iToast *toast = [[iToast alloc] initWithText:@"网络连接异常，请检查您的网络后重试"];
        [toast show];
//        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的网络连接已断开，请检查您的网络链接" preferredStyle:(UIAlertControllerStyleAlert)];
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleCancel) handler:nil];
        //        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"设置" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        //            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        //        }];
//        [alertVC addAction:action];
        //        [alertVC addAction:action1];
//        [vc presentViewController:alertVC animated:YES completion:nil];
        
        return NO;
    }
    
    return YES;
}

@end
